<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 * Class other extends MY_Controller
 * @author xiejianwei
 * 移动端用户接口类 其他
 */
class other extends API_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('page_model', 'mpage');
        $this->load->model('feedback_model', 'mfeedback');
        $this->load->model('links_model', 'mlinks');
    }

	protected $rules = array(
        "list_get" => array(
            array(
                "field" => "token",
                "label" => "Token",
                "rules" => "trim",
            )
        ),
        "feed" => array(
            array(
                'field' => 'content',
                'label' => '内容',
                'rules' => 'trim|required',
            ),
            array(
                'field' => 'tel',
                'label' => '联系方式',
                'rules' => 'trim|required',
            ),
            array(
                "field" => "token",
                "label" => "Token",
                "rules" => "trim|required",
            )
        )
	);


    //关于我们
    public function about() {
        // 验证
        $this->form_validation->set_rules($this->rules['list_get']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            $where = array('cid'=>9);
            $list = $this->mpage->get_one($where);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $list;
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //购买协议
    public function about_xy() {
        // 验证
        $this->form_validation->set_rules($this->rules['list_get']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            $where = array('cid'=>10);
            $list = $this->mpage->get_one($where);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $list;
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
  //购买协议---服务协议
    public function about_ser() {
        // 验证
        $this->form_validation->set_rules($this->rules['list_get']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            $where = array('cid'=>11);
            $list = $this->mpage->get_one($where);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $list;
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
  //意见反馈
    public function feedback() {
        // 验证
        $this->form_validation->set_rules($this->rules['feed']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            if ($this->userinfo) { //更新会员表
                if(!isset($this->data['terminalNo'])){
                    $this->data['terminalNo']=0;
                }
                $info = $this->macc->get_one($this->userinfo['id'],'id,phone,uname');
                // 组装创建数据
                $create_data = array(
                    'tel' => $this->data['tel'],
                    'content' => $this->data['content'],
                    'uid' => $info['id'],
                    'title' => $info['uname'],
                    'timeline' => time()
                );
                // 创建数据
                if ($id = $this->mfeedback->create($create_data)) {
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = $id;
                } else {
                    $this->vdata['returnCode'] = '0011';
                    $this->vdata['returnInfo'] = '操作失败';
                }
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //分享信息
    public function share() {
        // 验证
        $this->form_validation->set_rules($this->rules['list_get']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            $where = array('audit'=>1);
            $list = $this->mlinks->get_one($where);
            // 拉取数据
            if (!empty($list)) {
                if(!empty($list['photo'])){
                    $url=tag_photo($list['photo']);
                    if(!empty($url)){
                        $list['photo']=UPLOAD_URL.tag_photo($list['photo']);
                    }else{
                        $list['photo']=null;
                    }

                }
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = $list;
            } else {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作失败';
                $this->vdata['content'] = array();
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //用户类型
    public function userType() {
        $this->load->model('coltypes_model', 'mcoltypes');
        $data = $this->mcoltypes->get_all(array('cid'=>1,'name'=>'ctype','show'=>1),'id,title','sort_id asc');
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '操作成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = $data;
        $this->_send_json($this->vdata);
    }
    //获取感兴趣行业
    public function interested() {
        $this->load->model('category_model', 'mcategory');
        $data = $this->mcategory->get_all(array('ctype'=>24,'audit'=>1),'id,title');
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '操作成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = $data;
        $this->_send_json($this->vdata);
    }
    //获取注册时公司下拉
    public function company() {
        //$this->load->model('board_model','mboard');
        //$this->load->model('company_model','mcompany');
        $company = array();
        $this->load->model('company_board_model','mcompany_board');
        if(isset($this->data['kw']) && $this->data['kw']) {
            $where['like'] = array('short',$this->data['kw']);
            $company = $this->mcompany_board->get_all($where);
        }
        if($company){
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $company;
        } else {
            $this->vdata['returnCode'] = '100005';
            $this->vdata['returnInfo'] = '搜索为空';
            $this->vdata['content'] = array();
        }
        $this->_send_json($this->vdata);
    }
    //平台优化接口
    public function combing() {
        $this->load->model('company_model','mcompany');
        $id = $this->data['id'];
        $company = $this->mcompany->get_one($id,'is_combing,status');
        if($company['is_combing'] && !$company['status']) {
            $this->vdata['returnCode'] = '100005';
            $this->vdata['returnInfo'] = '您的申请已受理，客服将尽快与您联系';
            $this->vdata['content'] = array();
        } else {
            $is_combing = $company['is_combing'] ? 0 : 1;
            if($company['status']) {
                $is_combing = 1;
            }
            $combing = $this->mcompany->set($id,array('is_combing'=>$is_combing,'status'=>0));
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '申请成功，客服将尽快与您联系';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $combing;
        }
        $this->_send_json($this->vdata);
    }
    //权限接口
    public function power() {
        //audit 0 未禁用 1 禁用
        //status 用户状态，0 审核不通过 1 正在审核 2 审核通过
        $this->_auto();
        $uid = $this->userinfo['id'];
        $re = array(
            'returnCode' => '200',
            'returnInfo' => '帐号正常',
        );
        if($this->userinfo['audit']) {
            $re = array(
                'returnCode' => '7000001',
                'returnInfo' => '您的帐号已被禁用',
                );
            //return $re;
            $re = array_merge($this->vdata, $re);

        } else {
            $account_status = $this->userinfo['status'];
            if(!$account_status) {
                $re = array(
                    'returnCode' => '7000002',
                    'returnInfo' => '您的帐号审核未通过',
                    'timeline' => time());
                //return $re;
                $re = array_merge($this->vdata, $re);

            } elseif($account_status == 1) {
                $re = array(
                    'returnCode' => '7000003',
                    'returnInfo' => '您的账户将在24小时内审核',
                    'timeline' => time());
                $re = array_merge($this->vdata, $re);
                $this->power = 1;
                //return $re;
            } else {
                $this->power = 2;
            }
            $CI  = &get_instance();
            $CI->load->model('identity_model','midentity');
            $identity = $CI->midentity->get_one(array('uid'=>$uid));
            $identity_status = $identity ? $identity['status'] : 3;
            if($identity_status != 3) {
                //身份变更 审核状态 0 提交审核 1 审核不通过 2 审核通过
                if(!$identity_status) {
                    $re = array(
                        'returnCode' => '7000004',
                        'returnInfo' => '您的帐号身份变更正在审核',
                        'timeline' => time());
                    //return $re;
                    $re = array_merge($this->vdata, $re);
                    if($account_status == 1) {
                        $this->power = 1;
                    } elseif($account_status == 2 ) {
                        $this->power = 2;
                    }
                    //return $re;
                } elseif($identity_status == 1) {
                    $re = array(
                        'returnCode' => '7000005',
                        'returnInfo' => '您的帐号身份变更审核未通过',
                        'timeline' => time());
                    //return $re;
                    $re = array_merge($this->vdata, $re);
                    if($account_status == 1 || $account_status == 2) {
                        $this->power = 1;
                    }
                    //return $re;
                } else {
                    if($account_status == 1) {
                        $re = array(
                            'returnCode' => '7000003',
                            'returnInfo' => '您的账户将在24小时内审核',
                            'timeline' => time());
                        $re = array_merge($this->vdata, $re);
                        $this->power = 1;
                        //return $re;
                    } elseif($account_status == 2) {
                        $this->power = 2;
                    }
                }
            }
        }
        if($re) {
            $re['secure'] = JSON_SECURE;
            $re['content'] = '';

            $this->_send_json($re);
        }
    }
    public function base64_file() {
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $this->data['base64_content'], $result)){
            $type = $result[2];
            $file = time();
            $new_file = UPLOAD_PATH."/$file.{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $this->data['base64_content'])))){
                echo '新文件保存成功：', $new_file;
            }

        }
    }
    //app版本号管理
    public function version() {
        $this->load->model('app_model','mapp');
        $version = $this->data['version'];
        //43 IOS  44 Android
        $app = $this->mapp->get_one(array('ctype'=>$this->data['ctype']),'version,url,ctype');
        if($app['version'] > $version) {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '请求成功';
            $this->vdata['content'] = $app;
        } else {
            $this->vdata['returnCode'] = '100001';
            $this->vdata['returnInfo'] = '无更新版本';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = '';
        }
        $this->_send_json($this->vdata);
    }
    //合格投资人
    public function board() {
        $this->_auto();
        $account = $this->macc->get_one($this->userinfo['id'],'board');
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '请求成功';
        $this->vdata['content'] = $account;

        $this->_send_json($this->vdata);
    }
}
